package net.fpeg.kotlin.msa.wordbase.service

import net.fpeg.kotlin.msa.wordbase.dao.WordUserDao
import net.fpeg.kotlin.msa.wordbase.entity.WordBase
import net.fpeg.kotlin.msa.wordbase.entity.WordUser
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class WordUserService {

    @Autowired
    lateinit var wordUserDao: WordUserDao

    @Autowired
    lateinit var wordTagService: WordTagService

    @Autowired
    lateinit var wordUserRelationTypeService: WordUserRelationTypeService

    @Autowired
    lateinit var wordSourceService: WordSourceService

    fun getOne(id: Long) = wordUserDao.getById(id) ?: throw Exception("用户不存在")

    fun add(uid: Long) = wordUserDao.save(
            WordUser(uid, setOf(), setOf(), setOf(), setOf(), setOf())
    ).apply {
        val xdefaultTag = wordTagService.add(uid, "默认")
        val xdefaultSource = wordSourceService.add(uid, null, "根来源")
        val xdefaultRelationType = wordUserRelationTypeService.add(uid, "默认关联")
        defaultTag = xdefaultTag
        defaultSource = xdefaultSource
        defaultRelationType = xdefaultRelationType
        wordTags.plus(xdefaultTag)
        wordSources.plus(xdefaultSource)
        wordUserRelationTypes.plus(xdefaultRelationType)
    }.let { wordUserDao.save(it) }

}